const express = require('express')
const app = express()
const cors = require('cors')

// 商品数据
let list = [
    {
        id: 123,
        name: '保时捷911',
        total: 123,
        already: 122.99,
        url: './img/911.png',
        logs: [122.99]
    },
    {
        id: 666,
        name: '保时捷911',
        total: 666,
        already: 600,
        url: './img/911.png',
        logs: [600]
    }
]
// 运行跨域
app.use(cors())

// 获取商品信息
app.get('/goods', (req, res) => {
    let id = req.query.id
    let goods = list.filter(g => g.id == id)[0]
    res.send(goods)

})
// 砍一刀，算出一刀砍掉多少
app.get('/kyd', (req, res) => {
    let id = req.query.id
    let goods = list.filter(g => g.id == id)[0]

    let y = 0.1;
    let r = Math.random()
    switch (true) { 
        case r < 0.003515625: y = 0.9;break;
        case r < 0.00703125: y = 0.8;break;
        case r < 0.0140625: y = 0.7;break;
        case r < 0.028125: y = 0.6;break;
        case r < 0.05625: y = 0.5;break;
        case r < 0.1125: y = 0.4;break;
        case r < 0.225: y = 0.3;break;
        case r < 0.45: y = 0.2;break;
        case r < 0.9: y = 0.1;break;
        default: y = 0.1;
    }
    console.log(`概率r:${r} --- y:${y}`)

    // 系数
    const ratio = 0.5;
    let kp = (goods.total - goods.already) * ratio * y
    kp = kp < 0.01 ? 0.01 : kp;
    goods.logs.push(kp)
    goods.already += kp;
    res.send(goods)
})

app.listen(3000, function () {
    console.log('server running🚆')
})